package org.oschina.api.user.mail.service;

import java.util.List;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.user.mail.dao.UserMailTypeDAO;
import org.oschina.api.user.mail.model.UserMailType;
import org.oschina.api.user.mail.vo.UserMailTypeVo;
import org.oschina.bean.Result;
import org.oschina.security.ISecurity;
import org.oschina.security.annotation.Require;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserMailTypeService extends BaseService {

	@Autowired
	private UserMailTypeDAO userMailTypeDAO;
	@Autowired
	private ISecurity security;
	
	/**
	 * 返回
	 * @return
	 */
	public List<UserMailTypeVo> findVo()
	{
		List<UserMailTypeVo> result=_findVo();
		if(!security.require(new String[]{Module.MODULE_48}, getToken(), false))
		{
			for(UserMailTypeVo typeVo:result)
				typeVo.setText(null);
		}
		return result;
	}
	
	/**
	 * 更新
	 * @param userMail
	 * @return
	 */
	@Require(authorization=Module.MODULE_48)
	public Result update(UserMailType userMailType)
	{
		if(_update(userMailType)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		return ResultUtil.getSuccess(userMailType);
	}
	
	/**
	 * 返回
	 * @return
	 */
	private List<UserMailTypeVo> _findVo()
	{
		return userMailTypeDAO.findVo();
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public UserMailTypeVo _findVoById(int id)
	{
		return userMailTypeDAO.findById(id);
	}
	
	/**
	 * 更新
	 * @param userMailType
	 * @return
	 */
	private int _update(UserMailType userMailType)
	{
		return userMailTypeDAO.update(userMailType);
	}
}